import math

print('#############定义函数，用户自定义的函数#############')


def my_abs(a):
    if a > 0:
        return a
    else:
        return -a


print(my_abs(-100))
print('#############定义函数，用户自定义的函数，return null可以不写null#############')


def return_null(a):
    if len(a) == 0:
        return
    else:
        return len(a)


print(return_null(''))
print('#############定义函数，空函数#############')


def enpty_function():
    print("pass用于占位符！")
    pass


enpty_function()
print('#############定义函数，参数检查#############')


def my_abs_check(a):
    if not isinstance(a, (int, float)):
        raise TypeError('错误的参数类型')
    if a > 0:
        return a
    else:
        return -a


print(my_abs_check(-100))
print('#############定义函数，返回多个参数，返回值是一个tuple，可以省略括号#############')


def return_more_params(x, y, angle, step):
    nx = x + step * math.sin(angle)
    ny = y + step * math.cos(angle)
    return nx, ny


print(return_more_params(300, 100, 0, 10))
print(math.sin(0))
print(math.cos(0))
print('#############定义函数，练习函数#############')
print('请定义一个函数quadratic(a, b, c)，接收3个参数，返回一元二次方程：ax2 + bx + c = 0的两个解')


def result(a, b, c):
    if not isinstance(a, (int, float)):
        raise TypeError('参数错误')
    if b * b - 4 * a * c >= 0:
        x = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)
        x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)
        if x == x2:
            return x
        else:
            return x, x2
    else:
        return


def quadratic(a, b, c):
    if not isinstance(a, (int, float)):
        raise TypeError('参数错误')
    if b * b - 4 * a * c >= 0:
        return result(a, b, c)
    else:
        return '方程无解！'


print(quadratic(1, -8, 15))
